<template>
  <div class="page-container" :class="containerClass">
    <!-- 页面头部 -->
    <div v-if="showHeader" class="page-header">
      <div class="page-header__content">
        <!-- 面包屑 -->
        <el-breadcrumb v-if="breadcrumb.length > 0" class="page-breadcrumb">
          <el-breadcrumb-item 
            v-for="item in breadcrumb" 
            :key="item.path || item.title"
            :to="item.path"
          >
            <el-icon v-if="item.icon">
              <component :is="item.icon" />
            </el-icon>
            {{ item.title }}
          </el-breadcrumb-item>
        </el-breadcrumb>
        
        <!-- 页面标题区域 -->
        <div class="page-title-area">
          <div class="page-title-main">
            <h1 v-if="title" class="page-title">
              <el-icon v-if="icon" class="page-icon">
                <component :is="icon" />
              </el-icon>
              {{ title }}
            </h1>
            <div v-if="description" class="page-description">{{ description }}</div>
          </div>
          
          <!-- 页面操作区域 -->
          <div v-if="$slots.actions" class="page-actions">
            <slot name="actions" />
          </div>
        </div>
        
        <!-- 页面标签/状态 -->
        <div v-if="$slots.tags || tags.length > 0" class="page-tags">
          <slot name="tags">
            <el-tag 
              v-for="tag in tags" 
              :key="tag.text"
              :type="tag.type"
              :effect="tag.effect"
              :size="tag.size"
            >
              {{ tag.text }}
            </el-tag>
          </slot>
        </div>
      </div>
    </div>
    
    <!-- 页面内容 -->
    <div class="page-content" :class="contentClass">
      <!-- 工具栏 -->
      <div v-if="$slots.toolbar" class="page-toolbar">
        <slot name="toolbar" />
      </div>
      
      <!-- 主要内容 -->
      <div class="page-main">
        <slot />
      </div>
      
      <!-- 底部内容 -->
      <div v-if="$slots.footer" class="page-footer">
        <slot name="footer" />
      </div>
    </div>
    
    <!-- 加载遮罩 -->
    <div v-if="loading" class="page-loading">
      <el-icon class="loading-icon is-loading">
        <Loading />
      </el-icon>
      <div v-if="loadingText" class="loading-text">{{ loadingText }}</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { Loading } from '@element-plus/icons-vue'

export interface BreadcrumbItem {
  title: string
  path?: string
  icon?: any
}

export interface PageTag {
  text: string
  type?: 'success' | 'info' | 'warning' | 'danger'
  effect?: 'dark' | 'light' | 'plain'
  size?: 'large' | 'default' | 'small'
}

export interface PageContainerProps {
  // 页面标题
  title?: string
  // 页面描述
  description?: string
  // 页面图标
  icon?: any
  // 面包屑
  breadcrumb?: BreadcrumbItem[]
  // 页面标签
  tags?: PageTag[]
  
  // 布局控制
  showHeader?: boolean
  fluid?: boolean
  padding?: string | number
  background?: string
  
  // 内容样式
  contentClass?: string | string[]
  
  // 加载状态
  loading?: boolean
  loadingText?: string
  
  // 响应式
  responsive?: boolean
}

const props = withDefaults(defineProps<PageContainerProps>(), {
  breadcrumb: () => [],
  tags: () => [],
  showHeader: true,
  fluid: false,
  padding: '20px',
  background: '#f5f7fa',
  loading: false,
  loadingText: '加载中...',
  responsive: true
})

// 容器样式类
const containerClass = computed(() => [
  {
    'page-container--fluid': props.fluid,
    'page-container--loading': props.loading,
    'page-container--responsive': props.responsive
  }
])

// 内容区域样式
const contentStyle = computed(() => {
  const style: Record<string, string> = {}
  
  if (props.padding) {
    style.padding = typeof props.padding === 'number' ? `${props.padding}px` : props.padding
  }
  
  if (props.background) {
    style.backgroundColor = props.background
  }
  
  return style
})
</script>

<style scoped>
.page-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background: var(--el-bg-color-page);
  position: relative;
}

.page-container--fluid {
  max-width: none;
}

.page-container--loading {
  pointer-events: none;
}

/* 页面头部 */
.page-header {
  background: #fff;
  border-bottom: 1px solid var(--el-border-color-lighter);
  position: sticky;
  top: 0;
  z-index: 10;
}

.page-header__content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px 20px;
}

.page-container--fluid .page-header__content {
  max-width: none;
  padding: 16px 24px;
}

/* 面包屑 */
.page-breadcrumb {
  margin-bottom: 12px;
}

.page-breadcrumb :deep(.el-breadcrumb__item) {
  display: flex;
  align-items: center;
}

.page-breadcrumb :deep(.el-icon) {
  margin-right: 4px;
}

/* 标题区域 */
.page-title-area {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 12px;
}

.page-title-main {
  flex: 1;
  min-width: 0;
}

.page-title {
  display: flex;
  align-items: center;
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  line-height: 1.3;
}

.page-icon {
  margin-right: 8px;
  font-size: 20px;
}

.page-description {
  margin-top: 8px;
  font-size: 14px;
  color: var(--el-text-color-regular);
  line-height: 1.5;
}

.page-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

/* 页面标签 */
.page-tags {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* 页面内容 */
.page-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: v-bind('contentStyle.padding');
  background: v-bind('contentStyle.backgroundColor');
}

.page-toolbar {
  margin-bottom: 16px;
}

.page-main {
  flex: 1;
  min-height: 0;
}

.page-footer {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--el-border-color-lighter);
  text-align: center;
  color: var(--el-text-color-regular);
  font-size: 14px;
}

/* 加载遮罩 */
.page-loading {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.8);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.loading-icon {
  font-size: 32px;
  color: var(--el-color-primary);
  margin-bottom: 12px;
}

.loading-text {
  font-size: 14px;
  color: var(--el-text-color-regular);
}

.is-loading {
  animation: rotating 2s linear infinite;
}

@keyframes rotating {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 响应式设计 */
.page-container--responsive {
  --page-padding: 20px;
}

@media (max-width: 768px) {
  .page-container--responsive {
    --page-padding: 12px;
  }
  
  .page-container--responsive .page-header__content {
    padding: 12px 16px;
  }
  
  .page-container--responsive .page-title-area {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }
  
  .page-container--responsive .page-title {
    font-size: 20px;
  }
  
  .page-container--responsive .page-actions {
    justify-content: stretch;
  }
  
  .page-container--responsive .page-actions > :deep(.el-button) {
    flex: 1;
  }
  
  .page-container--responsive .page-content {
    padding: var(--page-padding);
  }
}

@media (max-width: 480px) {
  .page-container--responsive .page-header__content {
    padding: 8px 12px;
  }
  
  .page-container--responsive .page-title {
    font-size: 18px;
  }
  
  .page-container--responsive .page-actions {
    flex-direction: column;
  }
  
  .page-container--responsive .page-tags {
    justify-content: center;
  }
  
  .page-container--responsive .page-content {
    padding: 12px;
  }
}

/* 暗黑主题适配 */
.dark .page-header {
  background: var(--el-bg-color);
  border-bottom-color: var(--el-border-color);
}

.dark .page-loading {
  background: rgba(0, 0, 0, 0.8);
}
</style>
